using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;


using My_HTTP;
using Helper;



namespace Miner_Yedion
{


    public partial class frmMain : Form
    {
        public API_Yedion Yedion=null;
        public string username = "";

        
        public frmMain()
        {
            InitializeComponent();
        }

        public void Load_Notebooks_List(rootNotebook[] Notebooks)
        {
            rootNotebook dest;
            listnotebooks.Items.Clear();
            if (Notebooks != null)
            {
                foreach (rootNotebook rec in Notebooks)
                {

                    dest = Yedion_XML_API.Instance.Find_Difference_Notebook(rec);
                    if (dest == null)
                        dest = rec;

                    ListViewItem listitem = new ListViewItem(dest.Name);
                    if (dest.Checked == false)
                        listitem.BackColor = Color.Yellow;
                    else
                        listitem.BackColor = Color.White;

                    listitem.SubItems.Add(dest.Lecturer);
                    listitem.SubItems.Add(dest.ExamNumber.ToString());
                    listitem.SubItems.Add(dest.LastDate.ToShortDateString());
                    listitem.SubItems.Add(dest.Grade.ToString());

                    listnotebooks.Items.Add(listitem);
                }

            }        

        }

        public void Load_Grades_List(rootSubject[] Grades)
        {
            rootSubject dest;
            listgrades.Items.Clear();
            if (Grades != null)
            {
                foreach (rootSubject rec in Grades)
                {

                    dest = Yedion_XML_API.Instance.Find_Difference_Subject(rec);
                    if (dest == null)
                        dest = rec;

                    ListViewItem listitem = new ListViewItem(dest.Name);
                    if (dest.Checked == false)
                        listitem.BackColor = Color.Yellow;
                    else
                        listitem.BackColor = Color.White;

                    listitem.SubItems.Add(dest.Type);
                    listitem.SubItems.Add(dest.Points.ToString());
                    listitem.SubItems.Add(dest.Hours.ToString());
                    listitem.SubItems.Add(dest.Grade.ToString());
                    listitem.SubItems.Add(dest.Notes);

                    listgrades.Items.Add(listitem);
                }

            }        
        }

        private void frmMain_Load(object sender, EventArgs e)
        {


                tabControl.Visible = false;
                statusStrip1.Visible = false;

                frmLogin login = new frmLogin(this);
            
                login.ShowDialog();
            

        }

        private void buttongrades_Click(object sender, EventArgs e)
        {
            buttongrades.Enabled = false;

            ThreadEvents.Instance.param1 = username;
            ThreadEvents.Instance.param2 = combogradesyearstart.SelectedItem.ToString();
            ThreadEvents.Instance.param3 = combogradesyearend.SelectedItem.ToString();

            ThreadEvents.Instance.RunCommand(Operation.O_Grades,this.Thread_Greades_End);
        
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {


            switch (tabControl.SelectedIndex)
            {
                case 0: {
                            combogradesyearend.SelectedIndex = 0;
                            combogradesyearstart.SelectedIndex = 0;
                        } break;
                 case 1:
                        {
                            combonotebooksyear.SelectedIndex = 0;
                            combonotebookssemester.SelectedIndex = 0;
                        } break;


            };

        }

        private void frmMain_Activated(object sender, EventArgs e)
        {

            tabControl_SelectedIndexChanged(this, e);

        }

        private void bnotebooksdo_Click(object sender, EventArgs e)
        {
            bnotebooksdo.Enabled = false;

            ThreadEvents.Instance.param1 = username;
            ThreadEvents.Instance.param2 = combonotebooksyear.SelectedItem.ToString();
            ThreadEvents.Instance.param3 = combonotebookssemester.SelectedIndex.ToString();

            ThreadEvents.Instance.RunCommand(Operation.O_Notebooks, Thread_NoteBooks_End);
            


            /*List<TMy_Notebooks_Record> MyBooks;

            MyBooks = Yedion.Get_All_Notebooks(username,combonotebooksyear.SelectedItem.ToString(),combonotebookssemester.SelectedIndex.ToString());

            listnotebooks.Items.Clear();
            if (MyBooks != null)
            {
                foreach (TMy_Notebooks_Record rec in MyBooks)
                {
                    ListViewItem listitem = new ListViewItem(rec.Course_Name);
                    listitem.SubItems.Add(rec.Lecturer_Name);
                    listitem.SubItems.Add(rec.ExamMoed);
                    listitem.SubItems.Add(rec.date);
                    listitem.SubItems.Add(rec.Grade);
                    //4,5
                    listitem.SubItems.Add(rec.OrigialExam);
                    listitem.SubItems.Add(rec.MyExam);

                    listnotebooks.Items.Add(listitem);
                }
            }
    */



        }

        private void checkNotebooks_CheckedChanged(object sender, EventArgs e)
        {

            foreach (ListViewItem l in listnotebooks.Items)
            {
                l.Checked = checkNotebooks.Checked;
            }

        }

        private void buttonDownloadBooks_Click(object sender, EventArgs e)
        {
            List<rootNotebook> List = new List<rootNotebook>();
            buttonDownloadBooks.Enabled = false;
            
            foreach (ListViewItem l in listnotebooks.Items)
            {
                if (l.Checked)
                {
                    /*
                    TMy_Notebooks_Record temp = new TMy_Notebooks_Record();
                    temp.Course_Name = l.Text;
                    temp.OrigialExam = l.SubItems[5].Text;
                    temp.MyExam =      l.SubItems[6].Text;
                    */
                    rootNotebook src = new rootNotebook();
                    rootNotebook dest;
                    src.Name = l.Text;
                    dest = Yedion_XML_API.Instance.Find_Difference_Notebook(src);
                    if (dest != null)
                        List.Add(dest);
                   
                }
            }

            if (List.Count > 0)
            { 
                ThreadEvents.Instance.Result_rootNotebook = null;
                ThreadEvents.Instance.Result_rootNotebook = new rootNotebook[List.Count];
                List.CopyTo(ThreadEvents.Instance.Result_rootNotebook);
                ThreadEvents.Instance.RunCommand(Operation.O_Notebooks_Download, Thread_Notebooks_Download_End);
            
            }
            //Yedion.Download_Selected_Notebooks(List, ProgressBar);


        }

        private void listgrades_ItemActivate(object sender, EventArgs e)
        {
            if (listgrades.FocusedItem != null)
            {
                listgrades.FocusedItem.BackColor = Color.White;
                Yedion_XML_API.Instance.Set_Subject_Check_Attribute(listgrades.FocusedItem.Text, true);
            }
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {

            Yedion_XML_API.Instance.Save_To_Xml();

        }

        public void Thread_NoteBooks_End()
        {
            bnotebooksdo.Enabled = true;
            rootNotebook[] MyNotebooks = null;
            MyNotebooks = ThreadEvents.Instance.Result_rootNotebook;
            Load_Notebooks_List(MyNotebooks);
            Yedion_XML_API.Instance.Add_Notebooks_To_Xml(MyNotebooks);
            Yedion_XML_API.Instance.Save_To_Xml();

        }              
        public void Thread_Greades_End()
        {
            buttongrades.Enabled = true;

            rootSubject[] MySubjects = null;
            MySubjects = ThreadEvents.Instance.Result_rootSubject;
            Load_Grades_List(MySubjects);
            Yedion_XML_API.Instance.Add_Subjects_To_Xml(MySubjects);
            Yedion_XML_API.Instance.Save_To_Xml();
        
        }
        public void Thread_Notebooks_Download_End()
        {
            buttonDownloadBooks.Enabled = true;


        }

        public void ProgressReport(int value)
        {
            ThreadEvents.Instance.ReportProgress(value);
        }

        private void listnotebooks_ItemActivate(object sender, EventArgs e)
        {

            if (listnotebooks.FocusedItem != null)
            {
                listnotebooks.FocusedItem.BackColor = Color.White;
                Yedion_XML_API.Instance.Set_Notebook_Check_Attribute(listnotebooks.FocusedItem.Text, true);
            }


        }
    }
}